package common.work;

import java.util.Arrays;
import java.util.HashMap;

public class UTF8Utils {
    public static HashMap<String,Integer> byteMap = new HashMap<String, Integer>();
    static {
        byteMap.put("0", 1);
        byteMap.put("1", 1);
        byteMap.put("2", 1);
        byteMap.put("3", 1);
        byteMap.put("4", 1);
        byteMap.put("5", 1);
        byteMap.put("6", 1);
        byteMap.put("7", 1);
        byteMap.put("c", 2);
        byteMap.put("d", 2);
        byteMap.put("e", 3);
        byteMap.put("f", 4);
}
    /**
     * 替换4字节UTF-8编码的字符
     * @param s 字符串
     * @return 已替换4字节UTF-8编码字符的字符串
     */
    public static String remove4BytesUTF8Char(String s) {
        byte[] bytes = s.getBytes();
        byte[] removedBytes = new byte[bytes.length];
        int index = 0;

        String hex = null;
        String firstChar = null;
        for (int i = 0; i < bytes.length; ) {
            //转16进制
            hex = common.UTF8Utils.byteToHex(bytes[i]);

            if (hex == null || hex.length() < 2) {
                System.out.println("fail to check whether contains 4 bytes char(1 byte hex char too short), default return false.");
                // todo, throw exception for this case
                return null;
            }

            firstChar = hex.substring(0, 1);

            if (byteMap.get(firstChar) == null) {
                System.out.println("fail to check whether contains 4 bytes char(no firstchar mapping), default return false.");
                // todo, throw exception for this case
                return null;
            }

            if (firstChar.equals("f")) {
                byte[] b2 = "*".getBytes();
                String re = common.UTF8Utils.bytesToHex(b2);
                for (int k = 0; k < b2.length; ) {
                    String items = re.substring(0, 1);
                    for (int j = 0; j < byteMap.get(items); j++) {
                        removedBytes[index++] = b2[k++];
                    }
                    for (int j = 0; j < byteMap.get(firstChar); j++) {
                        i++;
                    }
                }
            } else {
                for (int j = 0; j < byteMap.get(firstChar); j++) {
                    removedBytes[index++] = bytes[i++];
                }
            }
        }
        byte[] bytes1 = Arrays.copyOfRange(removedBytes, 0, index);
        String s4 = new String(bytes1);
        return s4;
    }

    public static void main(String[] args) {
        String s = ".你实习的不知考研的枯燥无味，你考研的不知工作的身心疲惫，你工作的不知\uD84C\uDEAD出国的花钱流水，你出国的不知保研的夜不能寐…谁都别把自己想得最累，在这样一个社会，家贫人丑就该多读书多干活多流泪。\n" +
                "\n" +
                "2.当一件事情需要通过\uD845\uDE8C辩论或者争吵来让对方屈服时，这不代表对方的不可理喻，而是自己的无能。\n" +
                "\n" +
                "\n\uD83D\uDE03 \uD83D\uDE09 \uD83D\uDE04 \uD83D\uDE03" +
                "3.如果上天没有给你想要的，不是你值得拥有\uD83D\uDE09更好的，而是你不配。别以为今天是你人生的低谷，在你未来的日子里它都能算的上是巅峰了。";
        System.out.println(UTF8Utils.remove4BytesUTF8Char(s));
        System.out.println("===========================");
        System.out.println(UTF8Utils.filterEmoji(s));

    }

    public static String filterEmoji (String s){
        return s.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "*");
    }
}
